package com.ek.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

import com.ek.Kademlia;

public class SimpleConsole {
    public static void main(String[] args) throws Exception {
        SimpleConsole console = new SimpleConsole();
        Kademlia dht = new Kademlia();
        dht.join();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        for (String line = reader.readLine(); line != null; line = reader.readLine()) {
            if (line.trim().equals(""))
                continue;
            if (line.contains("exit")) {
                dht.leave();
                System.exit(1);
                break;
            }
            String command = null;
            String key = null;
            String value = "";
            
            StringTokenizer tokenizer = new StringTokenizer(line, " ");
            if (tokenizer.hasMoreTokens()) command = tokenizer.nextToken();
            if (tokenizer.hasMoreTokens()) key = tokenizer.nextToken();
            if (tokenizer.hasMoreTokens()) value = tokenizer.nextToken();

            getCommand(command, dht, console).execute(key, value);
        }
    }

    private static Command getCommand(String command, Kademlia dht, SimpleConsole console) {
        if (command == null) return console.new Null();
        if (command.toLowerCase().startsWith("get")) return console.new Get(dht);
        if (command.toLowerCase().startsWith("put")) return console.new Put(dht);
        return console.new Help();
    }

    private interface Command {
        public void execute(String... keyAndValue);
    }

    private class Get implements Command {
        private final Kademlia dht;

        public Get(Kademlia dht) {
            this.dht = dht;
        }

        public void execute(String... keyAndValue) {
            System.out.println(dht.get(keyAndValue[0]));
        }

    }

    private class Put implements Command {
        private final Kademlia dht;

        public Put(Kademlia dht) {
            this.dht = dht;
        }

        public void execute(String... keyAndValue) {
            if (keyAndValue.length == 2)
                dht.put(keyAndValue[0], keyAndValue[1]);
            else
                System.out.println("attention please number of parameters is incorrect must be two!");
        }

    }

    private class Help implements Command {

        public void execute(String... keyAndValue) {
            System.out.println("*********************************");
            System.out.println("* The list of commands:         *");
            System.out.println("* get key                       *");
            System.out.println("* put key value                 *");
            System.out.println("* help                          *");
            System.out.println("* exit                          *");
            System.out.println("*********************************");
        }

    }
    
    private class Null implements Command {

        public void execute(String... keyAndValue) {
        }

    }

}
